package cn.com.lyj6851.modules.product.service.impl;

import cn.com.lyj6851.api.MyShopException;
import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.domain.PageResult;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.product.domain.YdyStoreProductRelation;
import cn.com.lyj6851.modules.product.service.YdyStoreProductRelationService;
import cn.com.lyj6851.modules.product.service.YdyStoreProductService;
import cn.com.lyj6851.modules.product.service.dto.YdyStoreProductRelationDto;
import cn.com.lyj6851.modules.product.service.dto.YdyStoreProductRelationQueryCriteria;
import cn.com.lyj6851.modules.product.service.mapper.YdyStoreProductRelationMapper;
import cn.com.lyj6851.modules.product.vo.YdyStoreProductRelationQueryVo;
import cn.com.lyj6851.modules.user.service.YdyUserService;
import cn.com.lyj6851.utils.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品点赞和收藏表 服务实现类
 * </p>
 *
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class YdyStoreProductRelationServiceImpl extends BaseServiceImpl<YdyStoreProductRelationMapper, YdyStoreProductRelation> implements YdyStoreProductRelationService {

    private final YdyStoreProductRelationMapper yxStoreProductRelationMapper;
    private final YdyStoreProductService storeProductService;
    private final YdyUserService userService;
    private final IGenerator generator;

    /**
     * 获取用户收藏列表
     * @param page page
     * @param limit limit
     * @param uid 用户id
     * @return list
     */
    @Override
    public List<YdyStoreProductRelationQueryVo> userCollectProduct(int page, int limit, Long uid, String type) {
        Page<YdyStoreProductRelation> pageModel = new Page<>(page, limit);
        List<YdyStoreProductRelationQueryVo> list = yxStoreProductRelationMapper.selectRelationList(pageModel,uid,type);
        return list;
    }

    /**
     * 添加收藏
     * @param productId 商品id
     * @param uid 用户id
     */
    @Override
    public void addRroductRelation(long productId,long uid) {
        if(isProductRelation(productId,uid)) {
            throw new MyShopException("已收藏");
        }
        YdyStoreProductRelation storeProductRelation = YdyStoreProductRelation.builder()
                .productId(productId)
                .uid(uid)
                .type("collect")
                .build();
        yxStoreProductRelationMapper.insert(storeProductRelation);
    }

    /**
     * 取消收藏
     * @param productId 商品id
     * @param uid 用户id
     */
    @Override
    public void delRroductRelation(long productId,long uid,String category) {
        YdyStoreProductRelation productRelation = this.lambdaQuery()
                .eq(YdyStoreProductRelation::getProductId,productId)
                .eq(YdyStoreProductRelation::getUid,uid)
                .eq(YdyStoreProductRelation::getType,category)
                .one();
        if(productRelation == null) {
            throw new MyShopException("已取消");
        }


        this.removeById(productRelation.getId());
    }


    /**
     * 是否收藏
     * @param productId 商品ID
     * @param uid 用户ID
     * @return Boolean
     */
    @Override
    public Boolean isProductRelation(long productId, long uid) {
        int count = yxStoreProductRelationMapper
                .selectCount(Wrappers.<YdyStoreProductRelation>lambdaQuery()
                        .eq(YdyStoreProductRelation::getUid,uid)
                        .eq(YdyStoreProductRelation::getType,"collect")
                        .eq(YdyStoreProductRelation::getProductId,productId));
        if(count > 0) {
            return true;
        }

        return false;
    }

    @Override
    //@Cacheable
    public PageResult<YdyStoreProductRelationDto> queryAll(YdyStoreProductRelationQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyStoreProductRelation> page = new PageInfo<>(queryAll(criteria));
        PageResult<YdyStoreProductRelationDto> relationDtoPageResult = generator.convertPageInfo(page, YdyStoreProductRelationDto.class);
        relationDtoPageResult.getContent().forEach(i ->{
            i.setProduct(storeProductService.getById(i.getProductId()));
            i.setUserName(userService.getYdyUserById(i.getUid()).getNickname());
        });
        return relationDtoPageResult;
    }


    @Override
    //@Cacheable
    public List<YdyStoreProductRelation> queryAll(YdyStoreProductRelationQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyStoreProductRelation.class, criteria));
    }


    @Override
    public void download(List<YdyStoreProductRelationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyStoreProductRelationDto yxStoreProductRelation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", yxStoreProductRelation.getUid());
            map.put("商品ID", yxStoreProductRelation.getProductId());
            map.put("类型(收藏(collect）、点赞(like))", yxStoreProductRelation.getType());
            map.put("某种类型的商品(普通商品、秒杀商品)", yxStoreProductRelation.getCategory());
            map.put("添加时间", yxStoreProductRelation.getCreateTime());
            map.put(" updateTime",  yxStoreProductRelation.getUpdateTime());
            map.put(" isDel",  yxStoreProductRelation.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
